var ws = require("nodejs-websocket")


// Scream server example: "hi" -> "HI!!!"
var server = ws.createServer(function (conn) {
    broadcast('new user coming')
    conn.on("text", function (str) {
        console.log("Received "+str)
        broadcast(str.toUpperCase()+"!!!")
    })
    conn.on("close", function (code, reason) {
        broadcast('some one left')
        console.log("Connection closed")
    })
    conn.on("error", function (err) {
        console.log("handle err")
    })
})

//广播
function broadcast(msg) {
    server.connections.forEach(function (conn) {
        conn.sendText(msg)
    })
}

server.listen(3000, function () {
    console.log('websocket server listen on 3000')
})

